{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c1ede8c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import h5py\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.image as mpimg\n",
    "import os\n",
    "import os.path as osp\n",
    "import imageio.v2 as imageio\n",
    "os.environ[\"OPENCV_IO_ENABLE_OPENEXR\"] = \"1\"\n",
    "\n",
    "from scipy.stats import describe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54b6dddc",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import re\n",
    "import cv2\n",
    "\n",
    "\n",
    "def load_pfm_file(file_path):\n",
    "    with open(file_path, \"rb\") as file:\n",
    "        header = file.readline().decode(\"UTF-8\").strip()\n",
    "\n",
    "        if header == \"PF\":\n",
    "            is_color = True\n",
    "        elif header == \"Pf\":\n",
    "            is_color = False\n",
    "        else:\n",
    "            raise ValueError(\"The provided file is not a valid PFM file.\")\n",
    "\n",
    "        dimensions = re.match(r\"^(\\d+)\\s(\\d+)\\s$\", file.readline().decode(\"UTF-8\"))\n",
    "        if dimensions:\n",
    "            img_width, img_height = map(int, dimensions.groups())\n",
    "        else:\n",
    "            raise ValueError(\"Invalid PFM header format.\")\n",
    "\n",
    "        endian_scale = float(file.readline().decode(\"UTF-8\").strip())\n",
    "        if endian_scale < 0:\n",
    "            dtype = \"<f\"  # little-endian\n",
    "        else:\n",
    "            dtype = \">f\"  # big-endian\n",
    "\n",
    "        data_buffer = file.read()\n",
    "        img_data = np.frombuffer(data_buffer, dtype=dtype)\n",
    "\n",
    "        if is_color:\n",
    "            img_data = np.reshape(img_data, (img_height, img_width, 3))\n",
    "        else:\n",
    "            img_data = np.reshape(img_data, (img_height, img_width))\n",
    "\n",
    "        img_data = cv2.flip(img_data, 0)\n",
    "\n",
    "    return img_data\n",
    "def _load_pose(path, ret_44=False):\n",
    "    f = open(path)\n",
    "    RT = np.loadtxt(f, skiprows=1, max_rows=4, dtype=np.float32)\n",
    "    assert RT.shape == (4, 4)\n",
    "    RT = np.linalg.inv(RT)  # world2cam to cam2world\n",
    "\n",
    "    K = np.loadtxt(f, skiprows=2, max_rows=3, dtype=np.float32)\n",
    "    assert K.shape == (3, 3)\n",
    "\n",
    "    if ret_44:\n",
    "        return K, RT\n",
    "    return K, RT[:3, :3], RT[:3, 3]  # , depth_uint8_to_f32\n",
    "def imread_cv2(path, options=cv2.IMREAD_COLOR):\n",
    "    \"\"\"Open an image or a depthmap with opencv-python.\"\"\"\n",
    "    if path.endswith((\".exr\", \"EXR\")):\n",
    "        options = cv2.IMREAD_ANYDEPTH\n",
    "    img = cv2.imread(path, options)\n",
    "    if img is None:\n",
    "        raise IOError(f\"Could not load image={path} with {options=}\")\n",
    "    if img.ndim == 3:\n",
    "        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n",
    "    return img"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "deb55a48",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# Scannet++ depth dataset\n",
    "s = 1\n",
    "# List of file paths\n",
    "scene_dir = \"/home/liucong/data/3d/blendedmvs_processed/blendedmvs/5bbb6eb2ea1cfa39f1af7e0c\"\n",
    "file_paths = sorted([f for f in os.listdir(scene_dir) if f.endswith(\"_rgb.png\")])\n",
    "cols = 2\n",
    "rows = 15\n",
    "interval = 1\n",
    "# Create a figure with 2 columns and 10 rows,dpi=100\n",
    "fig, axs = plt.subplots(rows, cols, figsize=(12, rows*4))\n",
    "\n",
    "# Iterate over the file paths and display the images\n",
    "for i, file_path in enumerate(file_paths[s:rows*cols*interval//2+s:interval]):\n",
    "    rgb_path = osp.join(scene_dir, file_path)\n",
    "    depth_path = rgb_path.replace(\"_rgb.png\", \"_depth.exr\")\n",
    "    cam_path = rgb_path.replace(\"_rgb.png\", \"_cam.npz\")\n",
    "\n",
    "    img = imread_cv2(rgb_path, cv2.IMREAD_COLOR)\n",
    "    depth = imread_cv2(depth_path, cv2.IMREAD_UNCHANGED)\n",
    "    desc = f'{depth.min()}, {depth.max()}, {depth.mean()}'\n",
    "    cam_file = np.load(cam_path)\n",
    "    cam_desc = f'{cam_file['pose'][:3, -1]}'\n",
    "\n",
    "    axs[i * 2 // cols , i * 2 % cols ].imshow(img)\n",
    "    axs[i * 2 // cols , i * 2 % cols].axis(\"off\")  # Hide axis ticks\n",
    "    axs[i * 2 // cols , i * 2 % cols].set_title(cam_desc)\n",
    "    axs[i * 2 // cols , i * 2 % cols + 1].imshow(depth)\n",
    "    axs[i * 2 // cols , i * 2 % cols + 1].axis(\"off\")  # Hide axis ticks\n",
    "    axs[i * 2 // cols , i * 2 % cols + 1].set_title(desc)\n",
    "\n",
    "plt.show()\n",
    "# arbitrary pose\n",
    "# image masked when depth is 0.0\n",
    "# totally correct"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a531270",
   "metadata": {},
   "outputs": [],
   "source": [
    "# dfile = \"/lc/data/3D/blendedMVS/bmvs/5c1f33f1d33e1f2e4aa6dda4/rendered_depth_maps/00000105.pfm\"\n",
    "# file = open(dfile, \"rb\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c2ecfb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import PIL\n",
    "def process_image(scene_dir, rgb_path):\n",
    "\n",
    "    cam_path = rgb_path.replace(\"_rgb.png\", \"_cam.npz\")\n",
    "\n",
    "    rgb_image = imread_cv2(osp.join(scene_dir, rgb_path), cv2.IMREAD_COLOR) \n",
    "    cam_file = np.load(osp.join(scene_dir, cam_path))\n",
    "    intrinsics = cam_file[\"intrinsics\"].astype(np.float32)\n",
    "    camera_pose = cam_file[\"pose\"].astype(np.float32)\n",
    "    # plt.imshow(rgb_image)\n",
    "    # plt.show()\n",
    "    print(rgb_image.shape)\n",
    "    print(intrinsics)\n",
    "\n",
    "    image = PIL.Image.fromarray(rgb_image)\n",
    "    W, H = image.size\n",
    "    cx, cy = intrinsics[:2, 2].round().astype(int)\n",
    "    min_margin_x = min(cx, W - cx)\n",
    "    min_margin_y = min(cy, H - cy)\n",
    "    print(W, H)\n",
    "    print(cx, cy)\n",
    "    print(min_margin_x, min_margin_y)\n",
    "    print(min_margin_x > W / 5, min_margin_y > H / 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43c13175",
   "metadata": {},
   "outputs": [],
   "source": [
    "scene_dir = \"/lc/data/3D/blendedMVS/blendedMVSpp/5a752d42acc41e2423f17674\"\n",
    "rgb_path = '00000408_rgb.png'\n",
    "process_image(scene_dir, rgb_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86907d4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "rgb_path = '00000409_rgb.png'\n",
    "process_image(scene_dir, rgb_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "709073f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# Scannet++ depth dataset\n",
    "s = 10\n",
    "# List of file paths\n",
    "root = \"/lc/data/3D/blendedMVS/blendedMVS/5ba19a8a360c7c30c1c169df/blended_images\"\n",
    "file_paths = sorted([f for f in os.listdir(root) if f.endswith(\"_masked.jpg\")])\n",
    "cols = 3\n",
    "rows = 12\n",
    "interval = 2\n",
    "# Create a figure with 2 columns and 10 rows,dpi=100\n",
    "fig, axs = plt.subplots(rows, cols, figsize=(12, rows*3))\n",
    "\n",
    "# Iterate over the file paths and display the images\n",
    "for i, file_path in enumerate(file_paths[s:rows*cols*interval//3+s:interval]):\n",
    "    mask = mpimg.imread(osp.join(root, file_path))\n",
    "    depth_f = osp.join(root.replace(\"blended_images\",\"rendered_depth_maps\"), file_path.replace(\"_masked\",\"\").replace(\".jpg\",\".pfm\"))\n",
    "    depth = load_pfm_file(depth_f)\n",
    "    img = mpimg.imread(osp.join(root, file_path.replace(\"_masked\",\"\")))\n",
    "    axs[i * 3 // cols , i * 3 % cols ].imshow(img)\n",
    "    axs[i * 3 // cols , i * 3 % cols].axis(\"off\")  # Hide axis ticks\n",
    "    axs[i * 3 // cols , i * 3 % cols].set_title(file_path.split('_')[0]+f\"_{i}\")\n",
    "    axs[i * 3 // cols , i * 3 % cols + 2].imshow(depth)\n",
    "    axs[i * 3 // cols , i * 3 % cols + 2].axis(\"off\")  # Hide axis ticks\n",
    "    axs[i * 3 // cols , i * 3 % cols + 2].set_title(file_path.split('_')[0]+\"_depth\")\n",
    "    axs[i * 3 // cols , i * 3 % cols + 1].imshow(mask)\n",
    "    axs[i * 3 // cols , i * 3 % cols + 1].axis(\"off\")  # Hide axis ticks\n",
    "    axs[i * 3 // cols , i * 3 % cols + 1].set_title(file_path.split('_')[0]+\"_mask\")\n",
    "\n",
    "plt.show()\n",
    "# arbitrary pose\n",
    "# image masked when depth is 0.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4cd8df9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "print(depth.shape, depth.dtype) \n",
    "print(describe(depth, axis=None))\n",
    "print((depth==0.0).sum())\n",
    "# (576, 768) float32\n",
    "# DescribeResult(nobs=442368, minmax=(np.float32(0.0), np.float32(1.8402007)), mean=np.float32(0.6513939), variance=np.float64(0.21236680771159042), skewness=np.float64(-0.10090269893407822), kurtosis=np.float32(-0.8594873))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2616181c",
   "metadata": {},
   "outputs": [],
   "source": [
    "intrinsics_in, RT = _load_pose(\n",
    "    osp.join(root.removesuffix(\"blended_images\"), \"cams\", file_path.replace(\"_masked.jpg\",\"_cam.txt\")), ret_44=True\n",
    ")\n",
    "print(intrinsics_in)\n",
    "print(RT)\n",
    "# [[806.505     0.      384.45374]\n",
    "#  [  0.      806.505   274.8975 ]\n",
    "#  [  0.        0.        1.     ]]\n",
    "# [[-0.14511657  0.15030788  0.97793084 -0.882887  ]\n",
    "#  [ 0.11592087  0.9841694  -0.13406424  0.38591346]\n",
    "#  [-0.98260087  0.09390782 -0.1602425   0.23382568]\n",
    "#  [ 0.          0.          0.          1.        ]]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "vggt",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
